<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-4-23
*   +----------------------------------------------------------------------
*   | Link ( http://www.goshopx.com  http://www.shopingj.com )
*   +----------------------------------------------------------------------
**/

	class AttributeAction extends BaseAction{
		protected $attr_group,$category,$attrcate;
		function _initialize(){
			parent::_initialize();
			$this->dao = M('Attribute');
			$this->attrcate = M('CategoryAttribute');
			$this->attrgroup = M('AttributeGroup');
			$this->proattr = M("ProductAttribute");
			$this->category = M('ProductCategoryTag');
			$cate = $this->category->field('id,name')->where(array('parent_id'=>'1','type'=>'1'))->select();
			$attrarray = $this->attrcate->select();
			$status = array('1'=>'有效','0'=>'无效');
			$modelname = array('sj'=>'手机','sjpj'=>'手机配件','hmpf'=>'号码批发','sjlh'=>'手机靓号','sjsp'=>'手机饰品');
			//获取模型
			$modelname = M("SystemConfig")->field('value,title')->where('element="modelname"')->select();
			$modelname = field2key($modelname,'value');
			$this->assign('modelname',$modelname);
			$this->assign('status',$status);
			$this->assign('attrarray',$attrarray);
		}
		
		/**
		 * (non-PHPdoc)
		 * @see BaseAction::index()
		 */
		function index() {
			$where = array();
			$where=parent::create_where($where);
			$count = $this->dao->where($where)->count();
			$page=new Page($count,$this->listrows);
			$offset = $page -> firstRow;
			$length = $page -> listRows;
			$list = $this->dao->where($where)->order($this->order)->limit($offset,$length)->select();
			$this->assign("totalCount",$count);
			$this->assign('list',$list);
			$this->display();
			//parent::dwz_list();
		}
		
		function add(){
			$type = $_GET['type'];
			
			$this->assign('type',$type);
			$this->display();
		}
		
		/*
		 * 批量添加分类
		 * @author TB
		 * @date 2013.12.19
		 * */
		function addcatattr(){
			$this->display();
		}
		

		/*
		 * 保存分类属性
		* @author TB
		* @date 2013.12.19
		* */
		function editcatattr(){
			if(!$_POST['category_id']){
				$this->_ajm(300,'请选择分类！');
			}
			$data=$this->dao->create();
			if($data===FALSE){
				$this->_ajm(300,'数据创建失败，请重试',0);
			}
			$id=$this->dao->add($data);
			if($id){
				//插入分类属性表
				$model = M('CategoryAttribute');
				$d['category_id'] = $_POST['category_id'];
				$d['attr_id'] = $id;
				$d['platform'] = 'ALL';
				$reid = $model->add($d);
				if($reid){
					$this->_ajm(200,'信息保存成功！');
				}else{
					$this->_ajm(300,'分类属性信息保存失败！');
				}
			}else{
				$this->_ajm(300,'属性信息保存失败！');
			}
		}
		
		

		/*
		 * 批量出入分类
		* @author TB
		* @date 2013.12.19
		* */
		function batchcatattr(){
			$attrids = $_POST['attr_id'];
			//$catid = $_POST['cateid'];//echo $catid;exit;
			if(!$_POST['cat_id'] || !$attrids){
				$this->_ajm(300,'请选择分类！');
			}
			$model = M('CategoryAttribute');
			$attrids = explode(',', $attrids);
			$catid = $_POST['cat_id'];
			foreach ($attrids as $id){
				$d['category_id'] = $where['category_id'] = $catid;
				$d['attr_id'] = $where['attr_id'] = $id;
				$d['platform'] = 'ALL';
				
				$res = $model->where($where)->find();
				if($res){
					$reid = 1;
					continue;					
				}
				$reid = $model->add($d);
				if($reid === false){
					$msg .= "【分类：$catid, 属性：$id 】 "; 
				}
			}
			if($reid){
				$this->_ajm(200,'保存成功！');
			}else{
				$this->_ajm(300,'保存失败！'.$msg);
			}
		}

		function delattr(){
			//step 1 删除产品和属性绑定关系
			$id=intval($_REQUEST['id']);
			$where['attribute_id'] = $id;
			$this->proattr->where($where)->delete();
			
			//step 2 删除分类和属性绑定关系
			$where['attr_id'] = $id;
			$this->attrcate->where($where)->delete();
					
			//step 3删除属性星系
			parent::delall($this->dao);
		}
		
		function delattrg(){
			parent::delall($this->attrgroup);
		}
		
		/*
		 * 批量删除分类属性
		 * @author tb
		 * @date 2014.02.08
		 * */
		function delattrcate(){
			parent::delall($this->attrcate);
		}
		

		/*
		 * 删除分类属性
		* @author TB
		* @date 2013.12.19
		* */
		function delcatattr(){
			$id = $_GET['id'];
			if($id){
				$dao = M('CategoryAttribute');
				$reid = $dao->delete($id);
				if($reid){
					$this->_ajm(200,'删除成功！',1);
				}else{
					$this->_ajm(300,'删除失败！');
				}
			}else{
				$this->_ajm(300,'请重新选择数据！');
			}
		}
		
		
		function attrgroup(){
			$where = array();
			$model = M('AttributeGroup');
			$count = $model->count()	;
			$this->assign ( "totalCount", $count );
			$page=new Page($count,$this->listrows);
			$where = parent::create_where($where);
			$list=$model->where($where)->limit($page->firstRow.','.$page->listRows)->select();
			$this->assign('glist',$list);
			$this->display();
		}
		
		function groupinfo(){
			$id = $_REQUEST['id'];
			$model = M('AttributeGroup');
			$attrids = $model->field('attr_ids')->where(array('id'=>$id))->find();
			$model = M('Attribute');
			$attrlist = $model->where(array('id'=>array('IN',"$attrids[attr_ids]")))->select();
// 			echo $model->getLastSql();
// 			dump($attrlist);
			$this->assign('attrlist',$attrlist);
			$this->display();
		}
		
		/**
		 * 
		 */
		function attgr(){		
			import('Com.Newpro');
			$newpro = new Newpro();
			$attrgroup = $newpro->attrgroup();
			$id = $_REQUEST['id'];
			$model = M('AttributeGroup');
			$temp = $model->where(array('id'=>$id))->find();
			$ids = explode(',', $temp['attr_ids']);
			$this->assign('attrlist',$attrgroup);
			$this->assign('ids',$ids);
			$this->assign('id',$id);
			$this->assign('attrgroup',$temp);
			$this->display();
		}

		
		/**
		 * 
		 */
		function upattrgroup(){
			$model = M('AttributeGroup');
			$data = array();
			$data['attr_ids'] = implode(',', $_REQUEST['ids']);
			$data['name'] = $_REQUEST['name'];
			$data['sequence'] = $_REQUEST['sequence'];
			$data['modelname'] = $_REQUEST['modelname'];
			$reid = FALSE;
			if($_REQUEST['id']){
				$data['id'] = $_REQUEST['id'];
				$reid = $model->save($data);
			}else{
				$reid = $model->add($data);
// 				echo $model->getLastSql();
			}
			if($reid){
				$this->_ajm(200,'提交成功!',0,U('Attribute/index'));
	        }else{
	        	$this->_ajm(300,'提交失败，请重试!',0);
	        }
		}
		
		function editattr(){
			parent::edit($this->dao);
		}
		
		
		
		/*
		 * tb
		 * 注释 系统中没有地方用到
		 * function editattrg(){
			$id=intval($_REQUEST['id']);
			if(empty($id)){
				$this->_ajm(300,'请选择一条需要修改的数据',0);
			}
			$info=$this->attr_group->where("id=".$id)->find();
			if(empty($info)){
				$this->_ajm(300,'操作错误，请重试',0);
			}
			$this->assign("info",$info);
			$this->display();
		}*/
		
		
		function addattrg(){
			$this->display('attgr');
		}
		
		/**
		 * 分类属性列表
		 */
		function categoryattr(){
			$cateid = $_POST['cat_id'];
			$model=M('CategoryAttribute');
			if($cateid){
				$where['category_id']=$cateid;
			}
			$count = $model->where($where)->count();
			if($count){
				$dao=D('Cat');
				$page = new Page ( $count, $this->listrows );
				$offset = $page->firstRow;
				$length = $page->listRows;
	
				$list = $dao->cat_attr($cateid,'ALL',false,'list',$offset.','.$length,$this->order);//print_r($list);			
				$this->assign('totalCount',$count);
				$this->assign('list',$list); 
			}
			$this->assign('cateid',$cateid);
			$this->assign("catname",$_POST['cat_name']);
			$this->display();
		}
		
		
		/**
		 * ajax 修改分类下属性的排序和适用平台
		 */
		function ajax_update(){
			$id = $_REQUEST['id'];
			$field = $_REQUEST['field'];
			$value= $_REQUEST['value'];
			$dao = $_REQUEST['dao'];
			if(!$id || !$field || !$value){return 0;}
		
			$data['id'] = $id;
			$data[$field] = $value;
			$dao = $dao ? M($dao) : $this->attrcate;
			$reid = $dao->save($data);
			if($reid>=0){
				echo 1;
			}else{
				echo 0;
			}
			die();
		}
		
		/*
		 * 查找
		* */
		function lookup(){
			if($_POST['name']){
				$maps['name'] = array('LIke','%'.$_POST['name'].'%');
			}
				
			$this->assign('name',$_POST['name']);
			parent::lookup($maps);
				
		}
		
	}